// HidLib.h

#pragma once

#include "HidDevice.h"

using namespace System;

namespace HidLib 
{
	ref class HidDevice;

	public delegate void HidDeviceStatusChanged();

	public ref class HidDeviceEventMonitor
	{
	public:
		HidDeviceEventMonitor(HidDevice^ Device);

		event HidDeviceStatusChanged^ OnInsert
		{
			void add(HidDeviceStatusChanged^ Del)
			{
				_OnInsert += Del;
			}
			void remove(HidDeviceStatusChanged^ Del)
			{
				_OnInsert -= Del;
			}
			void raise()
			{
				HidDeviceStatusChanged^ Tmp = _OnInsert;
				if(Tmp)
				{
					Tmp->Invoke();
				}
			}
		}
		event HidDeviceStatusChanged^ OnRemove
		{
			void add(HidDeviceStatusChanged^ Del)
			{
				_OnRemove += Del;
			}
			void remove(HidDeviceStatusChanged^ Del)
			{
				_OnRemove -= Del;
			}
			void raise()
			{
				HidDeviceStatusChanged^ Tmp = _OnRemove;
				if(Tmp)
				{
					Tmp->Invoke();
				}
			}
		}

		property int PollingFrequency
		{
			int get()
			{
				return _PollingFrequency;
			}
			void set(int value)
			{
				_PollingFrequency = value;
			}
		}

		property bool Connected
		{
			bool get()
			{
				return _Connected;
			}
		};

		property bool Enabled
		{
			bool get()
			{
				return _Enabled;
			}
			void set(bool value)
			{
				if(value && !_Enabled)
					Init();
				_Enabled = value;
			}
		}

	private:
		delegate void EventMonitorDelegate();

		void Init();
		static void DisposeDeviceEventMonitor(IAsyncResult^ Ar);
		void DeviceEventMonitor(void);

		HidDevice^ _Device;
		int _PollingFrequency;
		bool _Connected;
		bool _Enabled;
		bool _DeviceWasConnected;

		HidDeviceStatusChanged^ _OnInsert;
		HidDeviceStatusChanged^ _OnRemove;
	};
}